import { query } from "express";
import { createConnection } from "mysql";
import { Application } from "pinus"
import { Dao } from "./dao"

export default function (app: Application) {
    return new RecordDao(app)
}

export class RecordDao extends Dao {

    public constructor(app: Application) {
        super(app)
    }
    insertBetRecord(values: any[]) {
        let sql = 'insert into lms_coin_record(user_id, user_name, round_id, bet, bet_type, settlement_multiple, pay_out, bet_time, finish_time, before_coin, after_coin, expand) values ?'
        return this.insert(sql, [values])
    }
    // 注单记录入库
    insertBetRecordMines(values: any[]) {
        let sql = 'insert into lms_coin_record(user_id, user_name, round_id, bet, bet_type, settlement_multiple, pay_out, bet_time, finish_time, before_coin, after_coin, expand) values( ? )'
        return this.insert(sql, [values])
    }
    // 游戏记录入库
    public insertGameRecordMines(values: any[]) {
        const sql = 'insert into lms_game_record_mines(user_id,round_id, win_fail, win_fail_coin, sys_map_data,click_map_data,choose_mines_num,choose_diamonds_num,endgame_at) values( ? )'
        return this.insert(sql, [values])
        // const args = [sql, [values]]
        // return this.insert(sql, args)
    }

    // 游戏记录入库
    public insertGameRecord(settlement: number, roundId: number, result: number, endType: number, operator: number) {
        const sql = 'insert into lms_game_record(round_id,settlement, result, billing_type, operator) values(?, ?, ?, ?, ?)'
        const args = [roundId, settlement, result, endType, operator]
        return this.insert(sql, args)
    }

    // 查询赔付排行（前20）
    public cashOutRank() {
        const sql = "SELECT user_name AS username, bet AS betCount, settlement_multiple AS multiple, pay_out AS cashOut FROM lms_coin_record where pay_out > 0 ORDER BY pay_out DESC LIMIT 20"
        const args = [];
        return this.select(sql, args)
    }


    // 查询最近下注信息(前20条)
    public cashOutMines() {
        // const sql = "SELECT user_name AS username, bet AS betCount, settlement_multiple AS multiple, pay_out AS cashOut FROM lms_coin_record where pay_out > 0 ORDER BY pay_out DESC LIMIT 20"
        let sql = 'SELECT '
            + 'a.round_id AS `index`,user_name AS userName,update_at AS finishTime,bet AS amount, pay_out as payout, '
            + 'settlement_multiple AS profit,b.sys_map_data AS mapData,b.click_map_data AS clickMapData '
            + 'FROM lms_coin_record a '
            + 'left join lms_game_record_mines b on a.round_id=b.round_id '
            + ' ORDER BY finishTime desc limit 15'
        const args = [];
        return this.select(sql, args)
    }

    // 查询倍率排行（前20）
    public multipleRank() {
        // const sql = "SELECT user_name AS username, bet AS betCount, settlement_multiple AS multiple, pay_out AS cashOut FROM lms_coin_record where settlement_multiple > 0 ORDER BY settlement_multiple DESC LIMIT 20"
        let sql = 'SELECT '
            + 'a.round_id AS `index`,user_name AS userName,update_at AS finishTime,bet AS amount, pay_out as payout, '
            + 'settlement_multiple AS profit,b.sys_map_data AS mapData,b.click_map_data AS clickMapData '
            + 'FROM lms_coin_record a '
            + 'left join lms_game_record_mines b on a.round_id=b.round_id '
            + ' ORDER BY pay_out desc limit 15'
        const args = [];
        return this.select(sql, args)
    }
    // 查询用户游戏记录(前20)
    public userRecord(uid: number) {
        // const sql = "SELECT user_name AS username, bet AS betCount, settlement_multiple AS multiple, pay_out AS cashOut, created_at AS date  FROM lms_coin_record where user_id = ? ORDER BY created_at DESC LIMIT 20"
        let sql = 'SELECT '
            + 'a.round_id AS `index`,user_name AS userName,update_at AS finishTime,bet AS amount, pay_out as payout, '
            + 'settlement_multiple AS profit,b.sys_map_data AS mapData,b.click_map_data AS clickMapData '
            + 'FROM lms_coin_record a '
            + 'left join lms_game_record_mines b on a.round_id=b.round_id '
            + 'where a.user_id=? ORDER BY finishTime desc limit 15'
        const args = [uid];
        return this.select(sql, args)
    }

    public previousRecord() {
        const sql = "SELECT user_name AS username, bet AS betCount, pay_out as cashOut, settlement_multiple AS multiple FROM lms_coin_record where round_id = (SELECT MAX(round_id) from lms_game_record) ORDER BY id DESC"
        return this.select(sql, [])
    }
}